package org.jypj.dev.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

import org.jypj.dev.dao.ITrainStationDao;
import org.jypj.dev.jsonbean.JsUtil;
import org.jypj.dev.model.TrainStation;
import org.jypj.dev.util.StringUtil;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.jypj.dev.model.Train;
import org.jypj.dev.model.TrainWork;
import org.jypj.dev.dao.ITrainDao;
import org.jypj.dev.dao.ITrainWorkDao;
import org.jypj.dev.service.ITrainService;
import org.jypj.dev.util.Page;

@Service("trainService")
public class TrainServiceImpl implements ITrainService {

    @Resource
    private ITrainDao trainDao;

    @Resource
    private ITrainWorkDao trainWorkDao;

    @Resource
    private ITrainStationDao trainStationDao;

    /**
     * 保存 字段为空的不存防止覆盖存在默认值的字段
     *
     * @param train
     * @return 保存后的对象包括ID
     */
    public int saveTrainByField(Train train) {
        String uuid = UUID.randomUUID().toString().replace("-", "").trim();
        train.setId(uuid);
        return trainDao.saveTrainByField(train);
    }

    /**
     * 保存 所有字段全都保存
     *
     * @param train
     * @return 保存后的对象包括ID
     */
    public int saveTrain(Train train) {
        int code = 0;
        try {
            String uuid = UUID.randomUUID().toString().replace("-", "").trim();
            train.setId(uuid);
            List<TrainWork> trainWorks = train.getTrainWorks();
            trainDao.saveTrain(train);
            if (trainWorks.size() > 0) {
                for (TrainWork trainWork : trainWorks) {
                    String uuid2 = UUID.randomUUID().toString().replace("-", "").trim();
                    trainWork.setId(uuid2);
                    trainWork.setTrainId(uuid);
                    if (StringUtil.isNotEmpty(train.getValidDate())) {
                        trainWork.setIsValid(0);
                        trainWork.setValidDate(train.getValidDate());
                    }
                    trainWork.setModifyTime(train.getModifyTime());
                    trainWorkDao.saveTrainWork(trainWork);
                }
            }
            List<TrainStation> trainStations = JsUtil.getJsResultByTrainCode(uuid, train.getCode());
            if (trainStations.size() > 0) {
                trainStationDao.saveTrainStations(trainStations);
            }
        } catch (DuplicateKeyException e) {
            code = -2;
        } catch (Exception e) {
            e.printStackTrace();
            code = -1;
        }
        return code;
    }

    /**
     * 根据ID删除
     *
     * @param id 主键ID
     * @return 删除记录数
     */
    public int deleteTrainById(String id) {
        TrainStation trainStation = new TrainStation();
        trainDao.deleteTrainById(id);
        trainStation.setTrainId(id);
        trainStationDao.deleteTrainStationByObject(trainStation);
        return 0;
    }

    /**
     * 根据对象删除
     *
     * @param train
     * @return
     */
    public int deleteTrainByObject(Train train) {

        return trainDao.deleteTrainByObject(train);
    }

    /**
     * 更新 只更新值不为空的字段
     *
     * @param train
     * @return 保存后的对象包括ID
     */
    public int updateTrainByField(Train train) {

        return trainDao.updateTrainByField(train);
    }

    /**
     * 更新 只更新值不为空的字段
     *
     * @return 保存后的对象包括ID
     */
    public int updateAllTrain(List<Train> trains) {
        int code = 0;
        for (Train train : trains) {
            trainDao.updateTrainByField(train);
        }
        return code;
    }

    /**
     * 更新 更新所有字段
     *
     * @param train
     * @return 保存后的对象包括ID
     */
    public int updateTrain(Train train) {
        int code = 0;
        try {
            TrainWork tw = new TrainWork();
            tw.setTrainId(train.getId());
            if (StringUtil.isNotEmpty(train.getValidDate())) {
                //删除掉已经存在的有效期之内的工序数据
                trainWorkDao.deleteTrainWorkByObject(tw);
            } else {
                // 将已经存在的工序变为无效
                trainWorkDao.updateTrainWork(tw);
            }
            // 在进行保存
            List<TrainWork> trainWorks = train.getTrainWorks();
            if (trainWorks.size() > 0) {
                for (TrainWork trainWork : trainWorks) {
                    String uuid2 = UUID.randomUUID().toString().replace("-", "").trim();
                    trainWork.setId(uuid2);
                    trainWork.setTrainId(train.getId());
                    if (StringUtil.isNotEmpty(train.getValidDate())) {
                        trainWork.setIsValid(0);
                        trainWork.setValidDate(train.getValidDate());
                    }
                    trainWork.setModifyTime(train.getModifyTime());
                    trainWorkDao.saveTrainWork(trainWork);
                }
            }
            trainDao.updateTrain(train);
            try {
                List<TrainStation> trainStations = JsUtil.getJsResultByTrainCode(train.getId(), train.getCode());
                TrainStation trainStation = new TrainStation();
                trainStation.setTrainId(train.getId());
                trainStationDao.deleteTrainStationByObject(trainStation);
                if (trainStations.size() > 0) {
                    trainStationDao.saveTrainStations(trainStations);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (DuplicateKeyException e) {
            code = -2;
        } catch (Exception e) {
            e.printStackTrace();
            code = -1;
        }
        return code;
    }

    /**
     * 按ID查询
     *
     * @return Train
     * @parm id 主键ID
     */
    public Train selectTrainById(String id) {

        return trainDao.selectTrainById(id);
    }

    /**
     * 分页查询 包含条件
     *
     * @param page 分页对象
     * @param map  查询条件
     * @return List<Train>
     */
    public Page selectOnePageByMap(Page page, Map<String, Object> map) {
        List<Train> trains = trainDao.selectOnePageByMap(page, map);
        if (trains != null && trains.size() > 0) {
            page.setResult(trains);
        } else {
            page.setResult(new ArrayList<Train>());
        }
        return page;
    }

    /**
     * 分页查询 包含对象条件
     *
     * @param page  分页对象
     * @param train 查询条件
     * @return Page
     */
    public Page selectOnePageByTrain(Page page, Train train) {
        List<Train> trains = trainDao.selectOnePageByTrain(page, train);
        if (trains != null && trains.size() > 0) {
            page.setResult(trains);
        } else {
            page.setResult(new ArrayList<Train>());
        }
        return page;
    }

    /**
     * 按条件查询全部的
     *
     * @param map 查询条件
     * @return List<Train>
     */
    public List<Train> selectAllByMap(Map<String, Object> map) {
        return trainDao.selectAllByMap(map);
    }

    /**
     * 按条件查询全部的
     *
     * @return List<Train>
     */
    public List<Train> selectAllByTrain(Train train) {

        return trainDao.selectAllByTrain(train);
    }

    /**
     * 按条件查询单个对象
     *
     * @param map 查询条件
     * @return Train
     */
    public Train selectObjectByMap(Map<String, Object> map) {

        return trainDao.selectObjectByMap(map);
    }

    /**
     * 按条件查询单个对象
     *
     * @return Train
     */
    public Train selectObjectByTrain(Train train) {

        return trainDao.selectObjectByTrain(train);
    }
}